package ces.frame.module.homePage.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ces.frame.module.homePage.dao.HomePageDAO;
import ces.frame.util.CommonUtil;
import ces.frame.util.Transition;
import ces.frame.util.anyPlace.APCommon;
import ces.frame.util.coral.tag.table.Page;
import ces.frame.util.internal.ICommon;

class HomePageBOImpl implements HomePageBO {

	public HomePageDAO homePageDAO;
	
	public HomePageDAO getHomePageDAO() {
		return homePageDAO;
	}

	public void setHomePageDAO(HomePageDAO homePageDAO) {
		this.homePageDAO = homePageDAO;
	}

	@Override
	public List<Map<String, Object>> getNotice(String state,String menuId,String rownum) throws Exception {
		return this.homePageDAO.getNotice(state,menuId,rownum);
	}

	@Override
	public List<Map<String, Object>> getImagesCenter(String code, String rownum)throws Exception {
		return this.homePageDAO.getImagesCenter(code, rownum);
	}

	@Override
	public List<Map<String, Object>> getFile(String id) throws Exception {
		return this.homePageDAO.getFile(id);
	}

	@Override
	public List<Map<String, Object>> getAtthFile(String code, String id,String fileType) throws Exception {
		return this.homePageDAO.getAtthFile(code,id,fileType);
	}

	@Override
	public Page seeAllData(Page page, String status, String menuId)
			throws Exception {
		return this.homePageDAO.seeAllData(page, status, menuId);
	}
	
	@Override
	public Page seeQzData(Page page)
			throws Exception {
		return this.homePageDAO.seeQzData(page);
	}

	@Override
	public Page seeAllImgData(Page page, String code) throws Exception {
		return this.homePageDAO.seeAllImgData(page, code);
	}

	@Override
	public List<Map<String, Object>> seeDetailed(String id) throws Exception {
		return this.homePageDAO.seeDetailed(id);
	}
	
	@Override
	public List<Map<String, Object>> seeQzDetailed(String id) throws Exception {
		return this.homePageDAO.seeQzDetailed(id);
	}
	
	@Override
	public List<Map<String, Object>> seeQzAtthDetailed(String fondId, String atthId) throws Exception {
		return this.homePageDAO.seeQzAtthDetailed(fondId, atthId);
	}

	@Override
	public Page getWorkTasks(Page page, String userId, String status, String where)
			throws Exception {
		return this.homePageDAO.getWorkTasks(page, userId, status, where);
	}

	@Override
	public int getWorkTaskCount(String userId)throws Exception {
		return this.homePageDAO.getWorkTaskCount(userId,"2,3,4");
	}

	@Override
	public Map<String, Object> getXmysCharts(String code, String year) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getXmysCharts(code, year);
		int[] xAxis = new int[list.size()];
		int[] series = new int[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = Integer.valueOf(APCommon.objToStr(list.get(i).get("Y")));
			series[i] = Integer.valueOf(APCommon.objToStr(list.get(i).get("S")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getArDataCharts(Map parmMap)throws Exception{
		List<Object[]> data = new ArrayList<Object[]>();
		
		String line = parmMap.get("line").toString();
		String codeStr = CommonUtil.getCodeStrByKey(line.split(":")[1]);//分类
		String[] codeArr = codeStr.split(",");
		
		String arType = parmMap.get("arType").toString();
		String[] arTypeArr = arType.split(",");
		String[] arCode = new String[arTypeArr.length];//档案类型code
		String[] arCodeName = new String[arTypeArr.length];//档案类型名称
		for(int i=0; i<arTypeArr.length; i++){
			arCode[i] = arTypeArr[i].split(":")[1].toString();
			arCodeName[i] = arTypeArr[i].split(":")[0].toString();
		}
		
		Object[] obj = new Object[arCode.length];
		for(int i=0; i<arCode.length; i++){
			List<Map<String,Object>> list = this.homePageDAO.getArData(arCode[i], line.split(":")[0], line.split(":")[1]);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put(arCodeName[i], list);
			obj[i] = map;
		}
		
		data.add(obj);
		data.add(line.split(":"));
		data.add(arCodeName);
		data.add(codeArr);
		return data;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getDaqkCharts(String year,int afterYear) throws Exception{
		List<Map<String,Object>> list = this.homePageDAO.getDaqkCharts(year,afterYear);
		List<Object[]> l = new ArrayList<Object[]>();
		String type[] = {"文书档案","规划档案","建筑档案","竣工档案","视频档案","照片档案"};
		String name = "";
		Object[] obj = null;
		int y = Integer.valueOf(year)+1;
		Object[] timeline = new Object[afterYear];
		Map<String,Object> m = null;
		//先初始化所有值
		int h = 0;
		for(int i=(y-afterYear); i<y; i++){
			timeline[h] = i;
			obj = new Object[type.length];
			for(int j=0; j<type.length; j++){
				m = new HashMap<String, Object>();
				m.put("value",0);
				m.put("name",type[j]);
				obj[j] = m;
			}
			l.add(obj);
			h++;
		}
		l.add(type);
		l.add(timeline);
		int nd = 0;
		int s = 0;
		int currentYear = 0;
		for(int i=0; i<list.size(); i++){
			m = list.get(i);
			name = APCommon.objToStr(m.get("X"));
			nd = Integer.valueOf(APCommon.objToStr(m.get("ND")));
			s = Integer.valueOf(APCommon.objToStr(m.get("S")));
			currentYear = afterYear - (y-nd);
			obj = l.get(currentYear);
			for(int j=0; j<obj.length; j++){
				m = (Map<String,Object>)obj[j];
				if(name.equals(m.get("name"))){
					m.put("value", s);
					l.get(currentYear)[j] = m;
				}
			}
			
		}
		return l;
	}

	@Override
	public Map<String, Object> getLysCharts(String year, int beforMonth) throws Exception{
		Map<String,Object> map = ICommon.getDateBefor(APCommon.strToDate(year+"-01", "yyyy-MM-dd"), beforMonth);
		List<Map<String,Object>> list = this.homePageDAO.getLysCharts(year, beforMonth);
		Object[] x = (Object[]) map.get("year");
		int[] jg = (int[]) map.get("jg");//竣工档案
		int[] jz = (int[]) map.get("jz");//建筑
		int[] gh = (int[]) map.get("gh");//规划
		String type = "";
		String[] name = new String[3];
		name[0] = "竣工档案";
		name[1] = "建筑档案";
		name[2] = "规划档案";
		int s = 0;
		for(int i=0; i<list.size(); i++){
			map = list.get(i);
			for(int j=0; j<x.length; j++){
				if(APCommon.objToStr(map.get("RQ")).equals(APCommon.objToStr(x[j]))){
					type = APCommon.objToStr(map.get("TYPE"));
					s =  APCommon.isNotNull(map.get("S"))?Integer.valueOf((map.get("S").toString())):0;
					if("竣工档案".equals(type)){
						jg[j] = s;
					}else if("建筑档案".equals(type)){
						jz[j] = s;
					}else if("规划档案".equals(type)){
						gh[j] = s;
					}
				}
			}
		}
		List<Object> l = new ArrayList<Object>(); 
		l.add(jg);
		l.add(jz);
		l.add(gh);
		map = new HashMap<String, Object>();
		map.put("x", x);
		map.put("name", name);
		map.put("value",l);
		return map;
	}

	@Override
	public Map<String, Object> getArchivesDetailed(String code, String id) throws Exception{
		return this.homePageDAO.getArchivesDetailed(code, id);
	}

	@Override
	public int setListShowRow(String row, String userId, String userName) throws Exception {
		return this.homePageDAO.setListShowRow(row, userId, userName);
	}

	@Override
	public Map<String, Object> getExborrowData() throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getExborrowData();
		String[] xAxis = new String[list.size()];
		int[] series = new int[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("MONTH")).substring(0, 7);
			series[i] = Integer.valueOf(APCommon.objToStr(list.get(i).get("SUM")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	@Override
	public Map<String, Object> getTemperature(String year) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getTemperature(year);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_"));
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
	@Override
	public Map<String, Object> getTemperatureBhg(String year) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getTemperatureBhg(year);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_"));
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
	@Override
	public Map<String, Object> getHumidity(String year) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getHumidity(year);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_"));
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	@Override
	public Map<String, Object> getHumidityBhg(String year) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getHumidityBhg(year);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_"));
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}

	@Override
	public Map<String, Object> getTemperature2(String month) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getTemperature2(month);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_")).substring(0, 10);
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	@Override
	public Map<String, Object> getTemperatureBhg2(String month) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getTemperatureBhg2(month);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_")).substring(0, 10);
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
	@Override
	public Map<String, Object> getHumidity2(String month) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getHumidity2(month);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_")).substring(0, 10);
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
	@Override
	public Map<String, Object> getHumidityBhg2(String month) {
		// TODO Auto-generated method stubhg
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getHumidityBhg2(month);
		String[] xAxis = new String[list.size()];
		float[] series = new float[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("DATE_")).substring(0, 10);
			series[i] = Float.parseFloat(APCommon.objToStr(list.get(i).get("VALUE_")));
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	
	@Override
	public List<Object[]> getArUseCharts(Map parmMap) throws Exception {
		// TODO Auto-generated method stub
		List<Object[]> data = new ArrayList<Object[]>();
		
		int num = Integer.parseInt(parmMap.get("num").toString());
		
		String arType = parmMap.get("arType").toString();
		String[] arTypeArr = arType.split(",");
		String[] arCode = new String[arTypeArr.length];//档案类型code
		String[] arCodeName = new String[arTypeArr.length];//档案类型名称
		for(int i=0; i<arTypeArr.length; i++){
			arCode[i] = arTypeArr[i].split(":")[1].toString();
			arCodeName[i] = arTypeArr[i].split(":")[0].toString();
		}
		
		List<Map<String,Object>> list = this.homePageDAO.getExborrowData();
		if(list==null||list.size()==0){
			return null;
		}
		String[] years = new String[num];
		years[num-1] = APCommon.objToStr(list.get(list.size()-1).get("MONTH"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		Date date = sdf.parse(years[years.length-1]);
		calendar.setTime(date);

		years[num-1] = sdf.format(calendar.getTime());
		for(int i=years.length-2; i>=0; i--){
			calendar.add(calendar.MONTH, -1);
			years[i] = sdf.format(calendar.getTime());
		}
		
		System.out.println();
		
		Object[] obj = null;
		for(int j=0; j<years.length; j++){
			obj = new Object[arCode.length];
			list = this.homePageDAO.getArUseCharts(years[j], arCode, arCodeName);
			for(int i=0; i<arCode.length; i++){
				Map<String, Object> vals = (Map<String, Object>)list.get(0);
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("value",vals.get(arCodeName[i]));
				m.put("name",arCodeName[i]);
				obj[i] = m;
			}
			years[j] = years[j] + "-01";
			data.add(obj);
		}
		
		data.add(arCodeName);
		data.add(years);
		return data;
	}

	@Override
	public List<Object[]> getStorageArCharts(Map parmMap) throws Exception {
		// TODO Auto-generated method stub
		List<Object[]> data = new ArrayList<Object[]>();
		
		//当前库房信息
		List<Map<String,Object>> list = this.homePageDAO.getStoreInfo();
		String[] storage = new String[list.size()];
		String[] storageId = new String[list.size()];
		Object[] obj = null;
		for(int i=0; i<list.size(); i++){
			Map<String, Object> vals = (Map<String, Object>)list.get(i);
			storage[i] = vals.get("STORAGE_NAME").toString();
			storageId[i] = vals.get("ID").toString();
			List<Map<String,Object>> listFondsArInfo = this.homePageDAO.getStoreArInfoByFons(storageId[i]);//获取该库房按全宗分组的统计数据
			if(listFondsArInfo==null){
				continue;
			}
			String[] fonds = new String[listFondsArInfo.size()];
			int[] fondsData = new int[listFondsArInfo.size()];
			obj = new Object[2];
			for(int j=0; j<listFondsArInfo.size(); j++){
				Map<String, Object> valsData = (Map<String, Object>)listFondsArInfo.get(j);
				fonds[j] = valsData.get("FONDS_NAME").toString() + "【" + valsData.get("QZH").toString() + "】";
				fondsData[j] = Integer.parseInt(valsData.get("SUM").toString());
			}
			
			obj[0] = fonds;
			obj[1] = fondsData;
			data.add(obj);
		}
		
		data.add(storage);
		data.add(storageId);
		return data;
	}

	@Override
	public Map<String, Object> getLiyongData() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getLiyongData();
		String[] xAxis = new String[list.size()];
		String[] series = new String[list.size()];
		for(int i=0; i<list.size(); i++){
			xAxis[i] = APCommon.objToStr(list.get(i).get("MONTH")).substring(0, 7);
			series[i] = String.valueOf(APCommon.objToStr(list.get(i).get("using_aim")));
		}
		System.out.println("xAxis:"+Arrays.toString(xAxis));
		System.out.println("series:"+Arrays.toString(series));
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	
	@Override
	public Map<String, Object> getQuanzongData(String type) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Map<String,Object>> list = this.homePageDAO.getQuanzongData(type);
		String[] ndArr = new String[list.size()];//年度
		String[] xAxis = new String[list.size()];//x轴
		String[] y = new String[list.size()];//年度个数
		
		for(int i=0; i<list.size(); i++){
			String[] types=((String) list.get(i).get("type")).split(",");
			xAxis[i] = types[0]+(String) list.get(i).get("qzmc")+types[1];
			y[i] = homePageDAO.getQuanzongNdData(types[1], types[0]);
			//List<String> listnd = homePageDAO.getQuanzongNdHData(types[1], types[0]);
			//ndArr[i] = listnd.toString();
		}
		System.out.println("xAxis:"+Arrays.toString(xAxis));
		System.out.println("y:"+Arrays.toString(y));

		map.put("xAxis", xAxis);
		map.put("y", y);
		//map.put("ndArr", ndArr);
		return map;
	}
}
